/*
 * ThreadMicroDelay.cpp
 *
 *  Created on: Jan 26, 2013
 *      Author: Drew Prevost
 */

#include "stdio.h"
#include "ThreadMicroDelay.h"

ThreadMicroDelay::ThreadMicroDelay()
    : Protothread()
{
  nMicrosStop = 0;
}

ThreadMicroDelay::~ThreadMicroDelay()
{
}

// set future timestamp to stop at, but only if the thread is not running,
// otherwise we keep pushing it further into the future and never get there.
void ThreadMicroDelay::setDelay( unsigned long nDelayMicros )
{
  if ( !isRunning() )
  {
    nMicrosStop = micros() + nDelayMicros;
  }
}

bool ThreadMicroDelay::run()
{
  PT_BEGIN()
  ;

  PT_WAIT_UNTIL( micros() >= nMicrosStop );

PT_END()
;
return false; // avoid compiler warning.  code not reached.
}

